// BaseList.cpp: implementation of the CBaseList class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BaseList.h"
//#include "DBPublic.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBaseList::CBaseList(const liveneo::tstring & AKeyName)
:CCustomElement( AKeyName)
{
	m_pItems=new CObList();
}

CBaseList::~CBaseList()
{
	Clear();
	delete m_pItems;
}

int CBaseList::GetCount()
{
	return m_pItems->GetCount();
}

CBaseElement* CBaseList::GetAt(int iIndex)
{
	if(iIndex<m_pItems->GetCount())
	{
		POSITION pos=m_pItems->FindIndex(iIndex);
		return (CBaseElement*)m_pItems->GetAt(pos);
	}
	return NULL;
	
}

void CBaseList::Delete(int iIndex)
{
	CBaseElement* ce=GetAt(iIndex);
	POSITION pos=m_pItems->FindIndex(iIndex);
	m_pItems->RemoveAt(pos);
	delete ce;
	ce=NULL;
}

void CBaseList::Clear()
{
	int iCount=GetCount();
	for(int i=0;i<iCount;i++)
	{
		Delete(0);	
	}
}

void CBaseList::AddHead(CBaseElement *element)
{
	m_pItems->AddHead((CObject*)element);
}

CBaseElement* CBaseList::GetItemByKey(liveneo::tstring ItemKey)
{
	int iCount=GetCount();
	for(int i=0;i<iCount;i++)
	{
		CBaseElement* ce=GetAt(i);
		if(ce->GetName()==ItemKey)
		{
			return ce;
		}

	}
	return NULL;
}

void CBaseList::AddTail(CBaseElement *element)
{
	m_pItems->AddTail((CObject*)element);
}

liveneo::tstring CBaseList::GetItemsXMLText()
{
	liveneo::tstring Buff;
	int iCount=GetCount();
    for (int i=0;i<iCount;i++)
    {
        if (i>0)
        {
            Buff += "\r\n\t";
        }
        Buff += GetAt(i)->GetXMLText();//static_cast<ThBaseElement *>(GetAt(i))->GetXMLText();
    }
    return Buff;
}

liveneo::tstring CBaseList::GetSubKeyName()
{
	return m_csSubKeyName;
}

void CBaseList::SetSubKeyName(liveneo::tstring name)
{
	m_csSubKeyName=name;
}

int CBaseList::Remove(CObject *obj)
{
	POSITION pos=m_pItems->Find(obj);
	CBaseElement* tElement=(CBaseElement*)obj;
	this->m_pXMLNode->RemoveChild(tElement->GetXMLNode());
	m_pItems->RemoveAt(pos);
	delete tElement;
	return 1;
}

liveneo::tstring CBaseList::GetXMLText()
{
	ParseXMLNode();
    if (this->GetXMLNode())
    {
        const liveneo::tstring CONFMT="<%s>\r\n\t%s\r\n</%0:s>";
		liveneo::tstring retstr;
		retstr=StringUtil::printf(CONFMT.c_str(),GetKeyName().c_str(),GetItemsXMLText().c_str());
		//retstr.Format(CONFMT,this->GetKeyName(),GetItemsXMLText());
        return retstr;
    }
    return "";
}

void CBaseList::ParseXMLNode()
{
	if (GetXMLNode() && !this->GetFIsParsed())
    {
        Clear();
        TiXmlElement * subxmlNode = GetXMLNode()->FirstChildElement(m_csSubKeyName.c_str());
        while (subxmlNode)
        {
            CBaseElement * basNode = NewNode();
            basNode->SetXMLNode(subxmlNode);
            basNode->ParseXMLNode();
            AddTail(basNode);
            subxmlNode = subxmlNode->NextSiblingElement(m_csSubKeyName.c_str());
			//WriteToLogFile("ParseXMLNode------------",true);
        }
        this->SetFIsParsed(true);
    }
}
